<template>
	<view>
		<view class="example">
			<!-- 基础表单校验 -->
			<uni-forms label-position="top" ref="valiForm" :rules="rules" :modelValue="valiFormData">
				<uni-forms-item label="标题" :required="false" name="title">
					<uni-easyinput v-model="valiFormData.title" placeholder="请输入标题" />
				</uni-forms-item>
				<uni-forms-item label="反馈内容" :required="false" name="content">
					<uni-easyinput type="textarea" v-model="valiFormData.content" placeholder="请输入反馈内容" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" @click="submit('valiForm')">提交</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				valiFormData: {
					uid: uni.getStorageSync('userInfo').id,
					title: '',
					content: ''
				},
				rules: {
					title: {
						rules: [{
							required: true,
							errorMessage: '标题不能为空'
						}]
					},
					content: {
						rules: [{
							required: true,
							errorMessage: '反馈内容不能为空'
						}]
					}
				},
			}
		},

		methods: {
			submit(ref) {
				this.$refs[ref].validate().then(res => {
					//console.log('success', res);
					// uni.showToast({
					// 	title: `校验通过`
					// })
					console.log("this.valiFormData", this.valiFormData)
					uni.request({
						url: "http://localhost:8080/user/opinion",
						header: {
							'Authorization': 'Bearer ' + wx.getStorageSync('token')
						},
						method: "post",
						data: this.valiFormData,

						success: (resp) => {
							//console.log("resp", resp)
							if (resp.data.code == 200) {
								uni.showToast({
									title: '反馈成功',
									icon: 'success',
									duration: 1000
								})
								setTimeout(() => {
									uni.switchTab({
										// 表单填写成功返回某页面
										url: '/pages/my/my'
									})
								}, 2000)
							}
						}
					})

				}).catch(err => {
					console.log('err', err);
				})
			},
		}
	}
</script>

<style>
	.example {
		padding: 15px;
		background-color: #fff;
	}
</style>